package com.atguigu.gmall.order.controller;

import com.atguigu.gmall.common.cache.GmallCacheOrder;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.dsig.keyinfo.RetrievalMethod;

@RestController
@RequestMapping(value = "/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 新增订单
     * @param orderInfo
     * @return
     */
    @GmallCacheOrder
    @PostMapping(value = "/addOrder")
    public Result<OrderInfo> addOrder(@RequestBody OrderInfo orderInfo){
        //获取用户id
        orderInfo.setUserId(3L);
//        String tradeNo = orderService.getTradeNo(3L + "");
//        if (!orderService.checkTradeCode(3L+"", tradeNo)){
//            return null;
//        }
        OrderInfo order = orderService.addOrder(orderInfo);
//        orderService.deleteTradeNo(3L+"");
        return Result.ok(order);
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @GetMapping(value = "/getById/{id}")
    public OrderInfo getById(@PathVariable(value = "id")Long id){
        return orderService.getById(id);
    }

    /**
     * 取消订单
     * @param id
     * @return
     */
    @GetMapping(value = "/cancelOrder/{id}")
    public Result cancelOrder(@PathVariable(value = "id")Long id){
        orderService.cancelOrder(id);
        return Result.ok();
    }
}
